package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.entity.CommentVideo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CommentVideoAdapter extends RecyclerView.Adapter<CommentVideoAdapter.CommentVideoHolder>{
    private Context context;
    private List<CommentVideo.HotCommentsBean> hotCommentsBeanList;

    public CommentVideoAdapter(Context context) {
        this.context = context;
    }
    public void setData( List<CommentVideo.HotCommentsBean> hotCommentsBeanList){
        this.hotCommentsBeanList = hotCommentsBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CommentVideoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment_video,parent,false);
        return new CommentVideoHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentVideoHolder holder, int position) {
        if(hotCommentsBeanList!=null) {
            holder.tips_comment.setVisibility(View.GONE);
            CommentVideo.HotCommentsBean hotCommentsBean = hotCommentsBeanList.get(position);
            holder.comment_content.setText(hotCommentsBean.getContent());
            String result = new SimpleDateFormat("yyyy-MM-dd").format(new Date(hotCommentsBean.getTime()));
            holder.comment_time.setText(result);
            holder.like_comment.setText(hotCommentsBean.getLikedCount()+" ");
            holder.like1.setImageResource(R.mipmap.like);
            holder.name_comment.setText(hotCommentsBean.getUser().getNickname());
            RoundedCorners roundedCorners= new RoundedCorners(30);
            RequestOptions options= RequestOptions
                    .bitmapTransform(roundedCorners)
                    .circleCrop();
            Glide.with(context).load(hotCommentsBean.getUser().getAvatarUrl())
                    .centerCrop().apply(options).into(holder.img_comment);
        }
        else{
            holder.tips_comment.setVisibility(View.VISIBLE);
            holder.tips_comment.setText("暂无评论，快来抢沙发吧");
        }

    }

    @Override
    public int getItemCount() {
         return hotCommentsBeanList==null?1:hotCommentsBeanList.size();
    }

    class CommentVideoHolder extends RecyclerView.ViewHolder {
        ImageView img_comment;
        TextView name_comment;
        TextView comment_time;
        TextView like_comment;
        TextView comment_content;
        TextView tips_comment;
        ImageView like1;
        View view;
        public CommentVideoHolder(@NonNull View itemView) {
            super(itemView);
            img_comment = itemView.findViewById(R.id.img_comment);
            name_comment = itemView.findViewById(R.id.name_comment);
            comment_time = itemView.findViewById(R.id.comment_time);
            like_comment = itemView.findViewById(R.id.like_comment);
            view = itemView.findViewById(R.id.view);
            comment_content = itemView.findViewById(R.id.comment_content);
            like1 = itemView.findViewById(R.id.like1);
            tips_comment = itemView.findViewById(R.id.tips_comment);
        }
    }
}
